﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;

namespace BSCMFF.Web.Pages.BSC_Setup
{
    public partial class CascadingKPI : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dkpi.ShowButtons = false;
            dkpi.DisableTextBoxes();

            //EGS.DataBinding += new EventHandler(EGS_DataBinding);

            
        }

        //void EGS_DataBinding(object sender, EventArgs e)
        //{
        //    if (EGS.RadListBoxSelectedEmployees.Items.Count == 0)
        //    {
        //        RadButtonCreateLink.Visible = false;
        //    }
        //    else
        //    {
        //        RadButtonCreateLink.Visible = true;
        //    }
        //}

        protected void RadButtonCreateLink_Click(object sender, EventArgs e)
        {
            //RadButtonCreateLink.Visible = false;
            //KPI kpi = (KPI)RadGrid1.SelectedItems[0].DataItem;

            BSCEntities context = new BSCEntities();


            foreach (RadListBoxItem i in EGS.RadListBoxSelectedEmployees.Items)
            {
                User u = new User();
                u.ID = Int32.Parse(i.Value);
                u.Lastname = i.Text;

                Personal_KPI pKPI = new Personal_KPI();
                pKPI.Name = "(Personal) " + dkpi.KPI.Name;
                //pKPI.Description = dkpi.KPI.Description;
                pKPI.KPI_FK = dkpi.KPI.KPI_ID;
                pKPI.User_FK = u.ID;

                context.AddToPersonal_KPI(pKPI);
            }

            try
            {
                context.SaveChanges();

                //ShowConnectionTable("(Personal) " + dkpi.KPI.Name, EGS.RadListBoxSelectedEmployees.Items);

                UpdatePKPIs();
            }
            catch
            {
                throw new Exception();
            }
        }

        private void ShowConnectionTable(string p, RadListBoxItemCollection userItems)
        {
            BSCEntities context = new BSCEntities();
            List<User> users = new List<User>();
            //foreach (RadListBoxItem i in userItems)
            //{
            //    User u = new User();
            //    u.ID = Int32.Parse(i.Value);
            //    u.Lastname = i.Text;

            //    u.Personal_KPI = new System.Data.Objects.DataClasses.EntityCollection<Personal_KPI>();

            //    Personal_KPI pKPI = new Personal_KPI();

            //    pKPI.Name = dkpi.KPI.Name;

            //    u.Personal_KPI.Add(pKPI);
            //    users.Add(u);
            //}

            

            //int[] pKPIs = (from pkpi in context.Personal_KPI
            //                   where pkpi.KPI_FK == dkpi.KPI.KPI_ID
            //                   select pkpi.Personal_KPI_ID).ToArray<int>();

            //int[] usersID = (from uid in context.Personal_KPI
            //                   where pKPIs.Contains(uid.Personal_KPI_ID)
            //                   select uid.User_FK).ToArray<int>();

            //users = (from u in context.Users
            //                    where usersID.Contains(u.ID)
            //                    select u).ToList();

            //RadGridPKPIs.DataSource = users;
            //RadGridPKPIs.DataBind();
            //RadGridPKPIs.AutoGenerateColumns = true;
        }

       

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item.ItemIndex != -1) //create new
            {
                int kpiID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["KPI_ID"].ToString());
                
                BSCEntities context = new BSCEntities();

                dkpi.KPI  = (from kpi in context.KPIs
                            where kpi.KPI_ID == kpiID
                            select kpi).SingleOrDefault();
                
                dkpi.DataBind();

                PanelCascade.Visible = true;
                UpdatePKPIs();
            }
        }

        protected void RadGridPKPIs_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            UpdatePKPIs();
        }

        private void UpdatePKPIs()
        {
            BSCEntities context = new BSCEntities();

            int? selectedKPIID = dkpi.KPI.KPI_ID;

            List<Personal_KPI> pKPIs = (from pkpi in context.Personal_KPI.Include("User")
                                        where pkpi.KPI_FK != null && pkpi.KPI_FK == selectedKPIID
                                        select pkpi).ToList();
            RadGridPKPIs.DataSource = pKPIs;
            RadGridPKPIs.Rebind(); // DataBind();
        }
    }
}